<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 文章控制器
 */
class Article extends MY_Controller {

    function __construct() {
        parent::__construct();
    }
    //文章详情页
    function detail() {
        $article_id = isset($_GET['article_id']) ? trim($_GET['article_id']) : "";
        $user_id = isset($_GET['user_id']) ? decrypt_user_id(trim($_GET['user_id'])) : "";
        $article_id = decrypt_article_id($article_id);
        
        $query_params = "a.id,a.title,a.authorid,a.content,a.thumb,a.commentnum,tags_name,i.head_img,i.nickname,a.create_time";
        $item = $this->db->query("select $query_params from tan_bbs_article a left join tan_users_info i on i.user_id = a.authorid where a.id = $article_id and a.status = 1 order by a.id desc limit 1")->row_array();
        if(!$item){
            $this->display('deleted/index.html');exit;
        }
        
        $repeat_str = array("<br>",'<p><span style="font-size: 13.3333px;"></span></p>',"<p><br></p>","<p>&nbsp;</p>",'<p><span style="color: rgb(51, 51, 51);">&nbsp;</span></p>','<p><span style="color: rgb(62, 62, 62);">&nbsp;</span></p>','<p><strong><span style="font-size: 10.5pt; color: black;">&nbsp;</span></strong></p>','<p><span style="font-size: 10.5pt; color: black;">&nbsp;</span></p>');
        $item['content'] = str_replace($repeat_str, "", $item['content']);
        
        $tags = array();
        if($item['tags_name']){
            $tags_arr = explode(",", $item['tags_name']);
            foreach ($tags_arr as $v){
                $v = explode("_", $v);
                $tags[$v[1]] = array("tid"=>$v[1],"tname"=>$v[0]); 
            }
        }
        $item['tags'] = $tags;
        //判断是否关注
        $is_fan = $user_id ? ($this->db->query("select * from tan_bbs_fans where user_id = $item[authorid] and followuid = $user_id and is_del = 1 order by id desc limit 1")->row_array() ? 1 : 0) : 0;
        
        //打赏记录
        $reward = $this->db->select("r.user_id,r.target_id,r.create_time,u.head_img,u.nickname")
                ->from("bbs_reward r")
                ->join("users_info u","u.user_id = r.user_id","left")
                ->where(array("r.target_id"=>$article_id,"r.target_type"=>'article'))->order_by("r.id desc")->group_by("r.user_id")->get()->result_array();
        foreach ($reward as $key=>$val){
            $reward[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
        }
        $this->assign('reward', $reward);
        
        $item['create_time'] = $this->set_time_three($item['create_time']);
        $item['head_img'] = strstr($item['head_img'],"http") ? $item['head_img'] : $this->img_host.$item['head_img'];
        $item['is_fan'] = $is_fan;
        //增加文章阅读数
        if($item['authorid'] != $user_id){
            $this->db->trans_begin();
            $query = $this->db->query("update tan_bbs_article set readnum = readnum + 1 where id = ".$article_id);
            if($query){
                $this->db->trans_commit();
            }else{
                $this->db->trans_rollback();
            }
        }
        //你可能还喜欢的文章
        $like = "(";
        $count = count($tags);
        $index = 0;
        foreach ($tags as $key => $tag){
            $index += 1;
            if($count == $index){
                $like .= " (a.tags_name like '%$tag[tname]%')";
            }else{
                $like .= " (a.tags_name like '%$tag[tname]%') or ";
            }
        }
        $like .= ")";
        $like_article = $this->db->query("select a.id,a.thumb,a.title,a.authorid,a.commentnum,a.readnum,u.head_img,u.nickname from tan_bbs_article a left join tan_users_info u on u.user_id = a.authorid where a.id not in($article_id) and $like order by a.sort desc limit 8")->result_array();
        foreach ($like_article as $key =>$val){
            $like_article[$key]['article_id'] = encrypt_article_id($val['id']);
            $like_article[$key]['authorid'] = encrypt_user_id($val['authorid']);
            $like_article[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
        }
        //大家说
        $where = array();
        $where['c.article_id'] = $article_id;
        $where['c.is_show'] = 1;
        $sayList = $this->db->select("c.*,u1.nickname comment_nickname,u1.head_img,u2.nickname reply_nickname")
            ->from("bbs_article_comment c")
            ->join("users_info u1","u1.user_id = c.uid","left")
            ->join("users_info u2","u2.user_id = c.repay_user_id","left")
            ->where($where)->order_by("id desc")->limit(10)->get()->result_array();
        foreach ($sayList as $key=>$val){
            if ($val['repay_user_id']){
                $sayList[$key]['is_reply'] = "1";
                $sayList[$key]['repay_user_id'] = encrypt_user_id($val['repay_user_id']);
            }else{
                $sayList[$key]['is_reply'] = "0";
            }
            $sayList[$key]['uid'] = encrypt_user_id($val['uid']);
            $lengther = mb_strlen($val['content'], "utf8");
            $sayList[$key]['content'] = $lengther > 30 ? $val['content']."..." : $val['content'];
            $sayList[$key]['create_time'] = $this->set_time_three($val['create_time']);
            $sayList[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
        }
        $this->assign("list",$sayList);
        $this->assign("like_article",$like_article);
        $this->assign("item", $item);
        $this->display('article/detail.html');
    }
}
